package com.ht.web.aduit;

import com.ht.bean.aduit.Aduitlog;
import com.ht.bean.aduit.Aduitmodel;
import com.ht.bean.aduit.Assess;
import com.ht.bean.aduit.Checkq;
import com.ht.bean.emp.Dep;
import com.ht.bean.emp.Empinfo;
import com.ht.bean.json.JsonData;
import com.ht.service.aduit.AssessService;
import com.ht.service.aduit.CheckqService;
import com.ht.service.education.TrialService;
import com.ht.service.emp.DepService;
import com.ht.service.emp.EmpinfoService;
import com.ht.util.Contants;
import com.ht.util.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 王金宝
 * @date 2020/6/29 10:04
 */
@Controller
@RequestMapping("check")
public class CheckqController {
    @Resource
    private JsonData jsonData;
    @Resource
    private CheckqService checkqService;
    @Resource
    private DepService depService;
    @Resource
    private EmpinfoService empinfoService;
    @Resource
    private TrialService trialService;

    //去到未打卡说明管理页面
    @RequestMapping("checkqlist")
    public String modellist(Map map){
        List<Empinfo> emplist = trialService.Emplist();//查询emp表
        map.put("elist",emplist);
        return "aduit/checkqlist";
    }

    /**
     * @param limit
     * @param page
     * @return
     * 获取未打卡列表信息
     */
    @RequestMapping("listdata")
    @ResponseBody
    public JsonData depList(int limit , int page,Checkq checkq){
        Pager pager = new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(checkqService.getTotalRow(checkq));
        jsonData.setData(checkqService.allPageCheckq(pager,checkq));
        return jsonData;
    }


    @RequestMapping("del")
    @ResponseBody
    public Boolean del(Integer id){
        int i = checkqService.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }

        return false;
    }


    //去到未打卡说明管理页面
    @RequestMapping("checkqlist2")
    public String datalist(){
        return "aduit/checkqlist2";
    }

    /**
     * @param limit
     * @param page
     * @return
     * 获取未打卡列表信息
     */
    @RequestMapping("listdata2")
    @ResponseBody
    public JsonData checkList(int limit , int page, HttpSession session){
        Pager pager = new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        Empinfo empinfo = (Empinfo) session.getAttribute("user");
        jsonData.setCount(checkqService.getTotalRowByEmpid(empinfo.getEmp_id()));
        jsonData.setData(checkqService.allPageCheckqByEmpid(empinfo.getEmp_id(),pager));
        return jsonData;
    }


    @RequestMapping("toadd")
    public String toadd(){
        return "aduit/checkadd";
    }

    @RequestMapping("add")
    @ResponseBody
    public Boolean add(Checkq checkq,String time,HttpSession session){
        checkq.setPunchtime(checkq.getPunchtime()+" "+time);
        checkq.setStatus(Contants.ADUIT_NOT);
        Empinfo empinfo = (Empinfo) session.getAttribute("user");
        checkq.setEmpid(empinfo.getEmp_id());
        Dep dep = depService.selectByPrimaryKey(empinfo.getDep_id());
        String empname = "";
        if(dep.getChairman() == empinfo.getEmp_id()){
            Dep dep1 = depService.getDepByFid(dep.getParentid());
            empname = empinfoService.selectByPrimaryKey(dep1.getChairman()).getEmp_name();
        }else {
            empname = empinfoService.selectByPrimaryKey(dep.getChairman()).getEmp_name();
        }
        checkq.setAuditperson(empname);
        int b = checkqService.insertSelective(checkq);
        if(b>0){
            return true;
        }
        return false;
    }

    @RequestMapping("toupd")
    public String toupd(Integer id,Map map){
        Checkq checkq = checkqService.selectByPrimaryKey(id);
        map.put("check",checkq);
        String[] array1=checkq.getPunchtime().split(" ");
        map.put("date",array1[0]);
        map.put("time",array1[1]);
        return "aduit/checkupd";
    }

    @RequestMapping("upd")
    @ResponseBody
    public Boolean upd(Checkq checkq,String time){
        checkq.setPunchtime(checkq.getPunchtime()+" "+time);
        int b = checkqService.updateByPrimaryKeySelective(checkq);
        if(b>0){
            return true;
        }
        return false;
    }
}
